﻿using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using BusinessBingo.Model;
using BusinessBingo.Web.Infrastructure.InfoStores.Interfaces;
using BusinessBingo.Web.Infrastructure.InfoStores.Keys;

namespace BusinessBingo.Web.Filters
{
    public class Localization : ActionFilterAttribute
    {
        private ICurrentLanguageStore _store;

        public Localization()
        {
        }

        public Localization(ICurrentLanguageStore languageStore)
        {
            this._store = languageStore;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // ToDo: Hack for Unit Testing
            if (HttpContext.Current != null)
            { 
                this._store = (ICurrentLanguageStore)DependencyResolver.Current.GetService(typeof(ICurrentLanguageStore));
            }

            LanguageKey key = this._store.GetPreferredLanguage();
            CultureInfo language = new CultureInfo(key.ToString());

            Thread.CurrentThread.CurrentCulture = language;
            Thread.CurrentThread.CurrentUICulture = language;
        }
    }
}